package com.vimhe.easyimport.util;

import com.vimhe.easyimport.reader.Excel2003Handler;
import com.vimhe.easyimport.reader.Excel2007Handler;
import com.vimhe.easyimport.reader.ExcelFormat;
import com.vimhe.easyimport.reader.IExcelReader;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Excel数据读取工具类
 *
 * @author Vimhe
 */
public final class ExcelReaderUtil {

    private ExcelReaderUtil() {
    }

    /**
     * 读取Excel文件
     *
     * @param reader   Excel数据读取器
     * @param fileName 文件全名
     * @throws IOException        e
     * @throws OpenXML4JException e
     * @throws SAXException       e
     */
    public static void readExcel(IExcelReader reader, String fileName)
            throws IOException, OpenXML4JException, SAXException {
        String suffix = fileName.substring(fileName.lastIndexOf('.') + 1).toUpperCase();

        if (ExcelFormat.XLS == ExcelFormat.valueOf(suffix)) {
            Excel2003Handler excel2003Handler = new Excel2003Handler(reader);
            excel2003Handler.process(fileName);
        } else if (ExcelFormat.XLSX == ExcelFormat.valueOf(suffix)) {
            Excel2007Handler excel2007Handler = new Excel2007Handler(reader);
            excel2007Handler.process(fileName);
        } else {
            throw new IOException("只支持格式为xls或xlsx的文件");
        }
    }

    /**
     * 读取Excel文件
     *
     * @param reader 读取器
     * @param file   文件对象
     * @throws IOException        e
     * @throws OpenXML4JException e
     * @throws SAXException       e
     */
    public static void readExcel(IExcelReader reader, File file)
            throws IOException, OpenXML4JException, SAXException {
        String fileName = file.getAbsolutePath();
        String suffix = fileName.substring(fileName.lastIndexOf('.') + 1).toUpperCase();

        if (ExcelFormat.XLS == ExcelFormat.valueOf(suffix)) {
            Excel2003Handler excel2003Handler = new Excel2003Handler(reader);
            excel2003Handler.process(file);
        } else if (ExcelFormat.XLSX == ExcelFormat.valueOf(suffix)) {
            Excel2007Handler excel2007Handler = new Excel2007Handler(reader);
            excel2007Handler.process(file);
        } else {
            throw new IOException("只支持格式为xls或xlsx的文件");
        }
    }

    /**
     * 读取Excel文件
     *
     * @param reader      读取器
     * @param excelFormat 文件格式
     * @param inputStream 文件流
     * @throws IOException        e
     * @throws OpenXML4JException e
     * @throws SAXException       e
     */
    public static void readExcel(IExcelReader reader, ExcelFormat excelFormat, InputStream inputStream)
            throws IOException, OpenXML4JException, SAXException {
        if (ExcelFormat.XLS == excelFormat) {
            Excel2003Handler excel2003Handler = new Excel2003Handler(reader);
            excel2003Handler.process(inputStream);
        } else if (ExcelFormat.XLSX == excelFormat) {
            Excel2007Handler excel2007Handler = new Excel2007Handler(reader);
            excel2007Handler.process(inputStream);
        } else {
            throw new IOException("只支持格式为xls或xlsx的文件");
        }
    }

}
